from com.rsa_util import RSAUtil
from com.aes_util import AESUtil

class ResponseDecryptor:
    @staticmethod
    def decrypt_response(encrypted_data: str, sk: str) -> str:
        """
        解密响应数据
        :param encrypted_data: 加密的响应数据
        :param sk: 加密的AES密钥（使用RSA公钥加密）
        :return: 解密后的响应数据
        """
        try:
            # 1. 使用RSA私钥解密sk，得到AES密钥
            aes_key = RSAUtil.decrypt_str(sk, RSAUtil.get_private_key())
            # 2. 使用AES密钥解密数据
            decrypted_data = AESUtil.decrypt_str(encrypted_data, aes_key)
            return decrypted_data
        except Exception as e:
            print(f"解密失败: {e}")
            raise RuntimeError(e)

def test_decryption(encrypted_data: str, sk: str):
    """
    测试解密功能
    :param encrypted_data: 加密的响应数据
    :param sk: 加密的AES密钥
    """
    try:
        print("=== 开始解密测试 ===")
        print(f"加密的SK: {sk}")
        print(f"加密的响应数据: {encrypted_data}")
        
        # 解密数据
        decrypted = ResponseDecryptor.decrypt_response(encrypted_data, sk)
        print(f"\n解密后的数据: {decrypted}")
        '''{"code":0,"msg":"成功","timestamp":1737508473065,"data":{"payString":null,"orderNo":"2025012210913710252","payOrderNo":"202501222267874970","payErrorCode":null,"payErrorMessage":null,"type":null,"waitPaymentDataNo":"202501221614542336"}}'''
        print("=== 解密测试完成 ===")
        
    except Exception as e:
        print(f"测试失败: {e}")
        raise

if __name__ == "__main__":
    
    # 这里的值需要替换为实际的加密数据和sk
    test_data = "95d0ec4de268e60165c3b7824ef2ff7b4cb9cce7281666574a7761ae8f2941c2b7f530042134befad450d9d8d3a3d4993c6e04a067a8bff2c3f8cdff8e8f5d2056931e1b84e8f1c405573630ff17c1f822bdfcca50b60bf44a0c01d59a21514336f88c3f28d960fd9f7f9de629f9390f61a14feb5474eeba42b92c02664650443b4491a746ab3fc4524612293117f47f6592a42d6588a6c9187845b0dc7899606b147e67f12ad47ffdb598af715625428a5a2a2aaa827d3d458d2c7a420409012e54e30b3b7cb1169441d52a0abcc53091932b350e63927534a480f08d5728a9b06baaf407a57108a8671b12697ddeb2ec1e00d3d60e53a32cdfea3a254b9523cdc9f2f74daacdd74db194b5c17bafc3df9b28b913badb205042a9073260ff4a9045932bd8f1959da668ab638676d94070f0c45108b2c0c44b0571f48c530bf4d36722ace024a3cefdec4693f914bc6e55f3022650d96438212a35ade4f9581e4c814385eb97419b45b7d654a42c44c2ca787b9f884c247a2ab3f3e41ef5fff1f6bd49d7c6bca213992489a1416453b9610359cf4578607d992f8daffbaf0e1dcd425165841a49522f2a48813ce2e8bff12c494c13ab119d30a1fa2e0fe4c6be53a54173559d03649e59eb22cc028379d83bab3a1529a51e169e4d8dd3cd75d6178c3cab5d121d5fba907dfbdc425512fc616b858881dfd2452eebbb27a2597e8164141ffe9ad432eb04fc43fb8c140c1d9a8ba5f2084d6fa10d8dd2299358fd8cf6234d01c3db6c8f3b89d4037adcab8c1d8e15af1f6e44ca50dfbfe5922c74c77693a41f002e427749a08e496bdcfa12eab0ff7e9d3718a1bd4265ce84c6948de7657fb494ef4630108273f66dad23cfe193efa29a8aa9d8e322e03ff2fb049a6a132400065b305f08bb256ff6617ca2c2dadd9a6e119b82070db5cf46770b08afaae7c8f94fb739961ea8c94a95c45c928943bc093d597b84655687254a71c9b5854fff26401b6c6adb617ac39aa2d5b72616a09ccdecb8fea8c990b26cca053bded6c1bdc8c7ef29b536252fd6c6b36e7a600b27addedf0ebdf193dc5c3070a1f7b0a206ae38a0abace0565c16bc70a5bb710dfab2fbf83bf2a8dec35c89b50caf8ec5c42f139303d540504637bdc5c510bb28e92b9888067e20e1ec9d8e6a7a1298516082b5980b6354bec588a1e9213ba9d6e4cd2dad510f2696ce9b8c4e9108b5a07cdcd7f15e9790d9a10146a67979ef77903b43db7b855efcb4fd7f8ffbd369941cbe26d313583623fdd44bc384aff554929270ac6ad566ebf24ff7dd0fc5249abc9920fa9b7318bd22bdc4cbbbc6ac1646d8ab4d3de82c0432976e41a744c0a2fcb210d0887f09f6a7efe01416d27cca4c9526d177a0d2920d8e4eb56ddeda9846f3a2e887c4d0538f1afb26f53804eb861319979c3d31ec4e4fd63a54d144214458d86493d0342fd92ea617b57347b20c3cc13b4ba016b0344a52cac6fe1762d903e9438d68defb6253c8fec8426841bcbff18bcf1977a2f2ebe50a5efc28424edee3f587eef112c4d3196851bd4d647f062a47944ed54efaf0f76f34e39ab553f9f41b50e9c712405f11bfb87aed23229f5940d909a4b60ed1cf267e33218990cf2e835ca151ee5643cd2943ca21bca4e68c3dff28f31b5d7434a1801fd745b6361a3b5ea8bc1d28e8ab26c11c1522b165a472aa50bdc8cc1e38"
    test_sk = "dLDH9PlC8uGZA13m50KGyrhmDEtwk/0ygNZOtNeP+pUNAtpML2uXN+C9chmzHaL1KAmtJIJ6o8crCV9qZwRehFuTRxy+RfXRcZ/v2vcTicwt5lZ/ko0Fv8PN87XFOOUEbRjzdkascn/dOSMuwrRR8NAdvR5S+6f5/hKrP2IrvSpGwrh64sDsDsafeMQBHazrKaVksSognIT/RpZbJiW9b+nKPv3bcZkCTVHJXhO5BonBaqDjqCUPj//YoKbz7c86dCe0bjcp69QBQpCzpjcq4KWHVxpe4V2vuF+2jMUaYq5stXrYdJ7nPEHvT6W+udOZm4Mqq2y0vtXs9/kZV+XIrg=="
    test_decryption(test_data, test_sk)

    test_data = "b3e28abb6f17ce329d57e67672a096c70b508c32ff6858ae108f8c7c4903d9af31b777bee2c9eab26a244ca3ba563ede239b405eb6562467b17f53d4a4a26b0231f185c379ede34cd0dbf8eeae70546be1e2f94242b5eb3caa962d48db2f1ee09a6ca7a3112babbb261ddb3edbace3ef90006dfa0484be8ba3126a6202c807a16799128a4922f3181cc8680fc882da8c0528fa9a507367b8c6f6d374600217de1898170ac595e99a10bf4e2ef5f0ba6aa1111da80c03bee08673123189616d1451409043104214b9ee2a788bdfe8019a08ebe330e464dc69cf298d2434bddc88df45acc2937a4cc91b371533ec443c9e5ce621366fa85a46f50b5ac525d017e175d688b65fb68fc5a58b35bf15c2fc5c5883bbc561fa40884a4f7fdeef96b960205893cd3c16791a94c19054bf87230d4ac8cfc1c3e439fab75f07351bf2be37f4290730f8295d394b3d8b86815bb97f9fffe43f4f789eda7154dc2e2aeef0022c5d01aea62e862708899f513188bc030bfd941a6d004bde9e9c651376318b1d3822c0b3b1c5acc240870a675d9e3245decc0ba110d0de22a4390af31b038a3b1bba6deda13dca0602f3b31b166d08bdec3d3c522a33ec42971bbd8a1ec587e51d8f68631b6ad394a9dc783e262206fabfb62dc3c7e72fc190f401da60c3ef414aa35b43f2b02fbf11a5a83e23fcc5a0ccdac32bdbe5fa3232e784a496802e57f99f2bea6efd66798526e690a43a785d47c740cd25bd1028b310522e3903852b45c6c577ccd2598034d87230b6afe842d1d5d260def887b2015d7ae92b4be337d0b55617672b567464b086c0281ccd209526cc0a44042bf1ac679c145cda8a81fc8697a01cd0be1826257b51f6dd1899e84c073fa7ae83a056fedab9704f6b24a4ed5888ebb18008a2152c7688793c5a4d5eef91d26d02b482c6eab14e118e5bc32b43ba31309543cf5652042096c417be82be9245bf9bb282b0ab5233236204acbafc4a4d34be2bec99b4cb24d76fd42f6281efdbf5b68acc8c1dc56cecc756d58f7f591430ada5760365d3af24edec03192c0f10d94f9edca8bdf8371b64643575509825d98090ba01f3d40dd51534ba3834a5e657e8dea9681af137b4248831b1d77076bc449b289971aac86f830e2636da0fda0efa8b9d982590184aaf95a73f5eeec63f3950e5a80baf165183cf14d4fda9c977e2c02bf7bddd52cf1191f7d94f8644b176a76eaae6c83c360e0e88eeea3f6e1d7dd643ce1479d18c73451a9b251e227b5507ab668d1eca0d81362811132e724e427c3ddd7c14525fd6749c06b1391db9c63d653a51a5a43182b7ceaf6ab2555838f34a9daf9ed1022ef0f5a7f2e2637ff13b4e5ef58cf6aedd36dec96cb147c55a454874436582c4ec35ba71dbbd97da5e1949be53c2a47b1aecc4aa6d911f18422f22c645f614d348a113f2137539c11f8115b70752b1cef9e6819dd78d7e923dcbb9dc71e10c0bf7ed0537f3f4f10157954fb97a924150cfbd0b871ff8c800abccfb0ea688fd555f982213fbdd46bbd45744419e543a7a6f55f17b59744d4930940b3eb81b4fe4c8bbd444e12c890706179c475706a818e2bc05fa7d2ca85d6d7e76c84029c6b4b1648f3bfb05a14bb4a0a034f1222893051d7578aa9b6511677d9d5df3ccb49bea65b1251db903d02789bf9b3f511761c26dbffc8e22c7cade8505208d60817b238dbf9d8ba1020efa33d109c0ef0e576ad7814f199b02f9e603297a409c8d3a2726"
    test_sk = "hXDO7qIFlwYc/YdKUZE61LlaRqTSuXtkD/WS5lSukuNS0WVWFxIlydNsJ/4iVZpyUCUekgjSd7n88VvWXwAHtcnTYmnZTNAtPe2AMQxR+eJBt62mgKd0yTog3/M1efgerTDcHbyXNs99W0UZnBXOllm0Ge01ZcjAeE73Vphz3kbhD59iEOA+BmfPhHK5gbFjsS4NQ0ryWyHWuLjSt1PlvOR1h0VZjNF0MrfBrWELFV2Uok8cjFF9Vu8AMr7Tg9jML06Xs6FzBj2uY3Rf2CbhYFPesBt409l3AWFrsaF8xOWz3Y78pRjgWUF6DWd3+4rvdwUjap0oxghGI4YsYw4+zg=="
    test_decryption(test_data, test_sk)

    